<template>
  <div class="person">
    <h2>姓名：{{ name }}</h2>
    <h2>年龄：{{ age }}</h2>
    <button @click="changeName">修改姓名</button>
    <button @click="changeAge">修改年龄</button>
    <button @click="showTel">查看联系方式</button>
    <hr>
    <h2>{{ c }}</h2>
  </div>
</template>

<script lang="ts">
/* setup函数优先执行，比beforecreate还早 */
export default {
  name: "Person",
  data() {
    return {
      a: 100,
      c: this.tel   /* data里面可以读取setup的数据,vue3非必要情况下不要在写data，method等 */
    }
  },
  methods: {
    b() {
      console.log('b');

    }
  },
  setup() {
    console.log(this); //setup函数中的this是undefined
    let name = 'zqs'
    let age = 18
    let tel = '18218392088'
    let c = '123'

    function changeName() {
      //name确实改变了，但是页面没有更新，因为name不是响应式数据
      name = 'zhangsan'
    }

    function changeAge() {
      age += 1
    }

    function showTel() {
      alert(tel)
    }

    return {
      name, age, changeName, changeAge, showTel, tel, c
    }
  },
}
</script>

<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0px 1px 2px #ddd;
  border-radius: 10px;
  padding: 20px;
}
</style>
